package demo.mianshi.thread;

public class _ABC {
    static volatile int i = 0;
        public void a(){
            synchronized (this){
                notify();
            }
        }
    public static void main(String[] args) {
        //编写一个程序，开启3个线程，这3个线程的ID分别为A、B、C，每个线程将自己的ID在屏幕上打印10遍，要求输出结果必须按ABC的顺序显示；如：ABCABC….依次递推
        Object lock = new Object();
        new Thread(() -> {
            synchronized (lock) {
                for (int j = 0; j < 10; j++) {
                    while (i != 0) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    i = 1;
                    lock.notifyAll();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                for (int j = 0; j < 10; j++) {
                    while (i != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    i = 2;
                    lock.notifyAll();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                for (int j = 0; j < 10; j++) {
                    while (i != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("C");
                    i = 0;
                    lock.notifyAll();
                }
            }
        }).start();
    }
}
